package org.ipccenter.balancechecker.task;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Callable;
import org.ipccenter.balancechecker.services.*;
import org.ipccenter.balancechecker.requests.*;

public class Task implements Callable<String> {
    private int balance;
    private ServiceType service;
    private ServiceRequest request;
    private int i = 0;

    public Task(ServiceType service, ServiceRequest request) {
        this.request = request;
        this.service = service;
    }

    public String call() {
        System.out.println( "Called at " + new SimpleDateFormat("HH:mm:ss.SSS").format (new Date()) + " from " + Thread.currentThread ().getName () +
        " for " + service.getClass().getName().substring(service.getClass().getName().lastIndexOf(".") + 1));
        try {
            while(!Thread.currentThread().isInterrupted()) {
                this.getRequest();
                Thread.sleep(1000);
                i++;
                if(i > 5 )
                    break;
            }
        } catch (InterruptedException e) {
            System.out.println("Thread has been interrupted");
        }
        return "Checking of a balance for " + service.getClass().getName().substring(service.getClass().getName().lastIndexOf(".") + 1) + " has ended.";
    }

    public void sendRequest() {
        request.sendRequest();
    }

    public void getRequest() {
        request.getRequest();
    }

}
